/**
 *  Copyright Matt Silbernagel 2010
 *  
 *	This file is part of MobilePrinting.
 *
 *	MobilePrinting is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MobilePrinting is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MobilePrinting.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.uc.labs.MobilePrinting.GUI;

import java.io.File;
import java.io.IOException;

import javax.swing.UIManager;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

import edu.uc.labs.MobilePrinting.Configurator;
import edu.uc.labs.MobilePrinting.Prefs.Preferences;
import edu.uc.labs.MobilePrinting.Prefs.PreferencesParser;

/**
 * 
 * @author Matt Silbernagel
 */
public class PreferencesPanel extends javax.swing.JPanel {

	public static boolean isOpen = false;

	/** Creates new form NewJPanel */
	public PreferencesPanel() {
		
		String user = Configurator.PREFS + Configurator.DELIM + Configurator.USER_CONF;
		String def  = Configurator.PREFS + Configurator.DELIM + Configurator.DEFAULT_CONF;
		
		
		
		File prefs = new File(user);
		if (!prefs.exists()) {
			prefs = new File(def);
		}
		try {
			PreferencesParser p_parser = new PreferencesParser();
			p = p_parser.parse(prefs);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		initComponents();
	}

	private void initComponents() {

		generalTabbedPane = new javax.swing.JTabbedPane();
		prefPanel = new javax.swing.JPanel();
		confServerLabel = new javax.swing.JLabel();
		confServerText = new javax.swing.JTextField(p.getConfigServer());
		jSeparator4 = new javax.swing.JSeparator();
		printerNameLabel = new javax.swing.JLabel();
		printerNameText = new javax.swing.JTextField(p.getPrinterName());
		jSeparator5 = new javax.swing.JSeparator();
		logLabel = new javax.swing.JLabel();
		
		logComboBox = new javax.swing.JComboBox();
		logLabel.setText("Log Level");
		logComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Debug", "Informational", "Error" }));
		logComboBox.setSelectedItem(p.getLogLevel());
		 
		jPanel1 = new javax.swing.JPanel();
		jPanel2 = new javax.swing.JPanel();
		
		updateLabel = new javax.swing.JLabel();
		updateCheckBox = new javax.swing.JCheckBox();
		updateCheckBox.setSelected(p.isAutoUpdate());
		updateCheckBox.setEnabled(false);
		
		startLabel = new javax.swing.JLabel();
		startCheckBox = new javax.swing.JCheckBox();
		startCheckBox.setSelected(p.isStartAtLogin());
		
		deleteLabel = new javax.swing.JLabel();
		deletePrinterCheckBox = new javax.swing.JCheckBox();
		deletePrinterCheckBox.setSelected(p.isDeleteOnExit());
		
		jSeparator3 = new javax.swing.JSeparator();
		jSeparator2 = new javax.swing.JSeparator();
		jSeparator1 = new javax.swing.JSeparator();
		
		confServerLabel.setLabelFor(confServerText);
		confServerLabel.setText("Configuration Server");
		
		printerNameLabel.setLabelFor(printerNameText);
		printerNameLabel.setText("Printer Name");
		
		
		javax.swing.GroupLayout prefPanelLayout = new javax.swing.GroupLayout(
				prefPanel);
		prefPanel.setLayout(prefPanelLayout);
		prefPanelLayout
				.setHorizontalGroup(prefPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								prefPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												prefPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jSeparator5,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																464,
																Short.MAX_VALUE)
														.addComponent(
																jSeparator4,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																464,
																Short.MAX_VALUE)
														.addComponent(
																confServerText,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																377,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(confServerLabel)
														.addComponent(printerNameLabel)
														.addComponent(
																printerNameText,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																377,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(logLabel)
														.addComponent(
																logComboBox,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																377,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));
		prefPanelLayout
				.setVerticalGroup(prefPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								prefPanelLayout
										.createSequentialGroup()
										.addGap(30, 30, 30)
										.addComponent(confServerLabel)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												confServerText,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addComponent(
												jSeparator4,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												10,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addComponent(printerNameLabel)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												printerNameText,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addComponent(
												jSeparator5,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addComponent(logLabel)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												logComboBox,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												26, Short.MAX_VALUE)

										.addContainerGap()));
		generalTabbedPane.addTab("General Preferences", jPanel1);
		generalTabbedPane.addTab("Advanced Preferences", prefPanel);

		updateCheckBox.setText("Auto Update");
		updateCheckBox.setToolTipText("Enable Automatic Updating\n");

		startCheckBox.setText("Start at Login");
		startCheckBox
				.setToolTipText("Check to start the application when the computer starts");
		startCheckBox.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				startCheckBoxActionPerformed(evt);
			}
		});

		deletePrinterCheckBox.setText("Delete Printer on Exit");
		deletePrinterCheckBox
				.setToolTipText("Check to uninstall printer when the application is exited");
		deletePrinterCheckBox.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				deletePrinterCheckBoxActionPerformed(evt);
			}
		});

		startLabel.setFont(new java.awt.Font("DejaVu Sans", 3, 12)); // NOI18N
		startLabel.setLabelFor(startCheckBox);
		startLabel
				.setText("Check to start the application when the computer starts");

		// jSeparator1.setForeground(new java.awt.Color(8, 7, 2));

		deleteLabel.setFont(new java.awt.Font("DejaVu Sans", 3, 12)); // NOI18N
		deleteLabel.setLabelFor(deletePrinterCheckBox);
		deleteLabel
				.setText("Check to uninstall printer when the application is exited");
		deleteLabel.setToolTipText("NULL");
		deleteLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);

		updateLabel.setFont(new java.awt.Font("DejaVu Sans", 3, 12));
		updateLabel.setLabelFor(updateCheckBox);
		updateLabel.setText("Enable Automatic Updating");

		// jButton3.setText("Restore Defaults");

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								startCheckBox)
																						.addComponent(
																								deletePrinterCheckBox)
																						.addComponent(
																								updateCheckBox,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								114,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jSeparator3,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								489,
																								Short.MAX_VALUE)
																						.addGroup(
																								jPanel2Layout
																										.createSequentialGroup()
																										.addGap(
																												12,
																												12,
																												12)
																										.addComponent(
																												updateLabel,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												403,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																												74,
																												javax.swing.GroupLayout.PREFERRED_SIZE))
																						.addComponent(
																								jSeparator2,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								489,
																								Short.MAX_VALUE)
																						.addGroup(
																								jPanel2Layout
																										.createSequentialGroup()
																										.addGap(
																												12,
																												12,
																												12)
																										.addComponent(
																												startLabel)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																												77,
																												javax.swing.GroupLayout.PREFERRED_SIZE))
																						.addComponent(
																								jSeparator1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								489,
																								Short.MAX_VALUE)
																						.addGroup(
																								jPanel2Layout
																										.createSequentialGroup()
																										.addGap(
																												12,
																												12,
																												12)
																										.addComponent(
																												deleteLabel,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												417,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																												60,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																							))))));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												startCheckBox,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												22,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(startLabel)
										.addGap(13, 13, 13)
										.addComponent(
												jSeparator1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(deletePrinterCheckBox)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(deleteLabel)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jSeparator2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(updateCheckBox)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(updateLabel)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jSeparator3,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addContainerGap(45, Short.MAX_VALUE)));
		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout.createSequentialGroup().addContainerGap()
						.addComponent(jPanel2,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout.createSequentialGroup().addContainerGap()
						.addComponent(jPanel2,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addContainerGap(371,
						Short.MAX_VALUE).addGap(18, 18, 18).addContainerGap())
				.addComponent(generalTabbedPane,
						javax.swing.GroupLayout.PREFERRED_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.PREFERRED_SIZE));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												generalTabbedPane,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												362,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												layout
														.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE))
										.addContainerGap(19, Short.MAX_VALUE)));
	}

	private void deletePrinterCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void startCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	public static void createAndShowGUI() {
		UIManager.put("swing.boldMetal", Boolean.FALSE);
		

		// Create and set up the window if the window doesn't
		// already exist
		
		
		PreferencesPanel panel = new PreferencesPanel();
		PreferencesFrame frame = new PreferencesFrame( panel );
		// Add content to the window.
		frame.add(panel);
		frame.setLocation(400, 200);
		// Display the window.
		frame.pack();
		if (PreferencesPanel.isOpen){
			frame.toFront();
			frame.repaint();
			// somehow bring it to the front;
			// or do nothing at all
		} else {
			logger.debug("The preferences pane is not currently open, opening...");
			frame.setVisible(true);
			PreferencesPanel.isOpen = true;
		}
	}
	
	

	// Variables declaration - do not modify
	public javax.swing.JCheckBox deletePrinterCheckBox;
	public javax.swing.JCheckBox startCheckBox;
	public javax.swing.JCheckBox updateCheckBox;
	public javax.swing.JComboBox logComboBox;
	public javax.swing.JLabel printerNameLabel;
	private javax.swing.JLabel deleteLabel;
	private javax.swing.JLabel startLabel;
	private javax.swing.JLabel updateLabel;
	private javax.swing.JLabel confServerLabel;
	private javax.swing.JLabel logLabel;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel prefPanel;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JSeparator jSeparator2;
	private javax.swing.JSeparator jSeparator3;
	private javax.swing.JSeparator jSeparator4;
	private javax.swing.JSeparator jSeparator5;
	private javax.swing.JTabbedPane generalTabbedPane;
	public javax.swing.JTextField confServerText;
	public javax.swing.JTextField printerNameText;
	
	private Preferences p;
	private static final long serialVersionUID = 7883092810671401781L;
	private static Logger logger = Configurator.LOGGER;
	// End of variables declaration

}
